package pub.tbc.api.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpRequest;
import lombok.extern.slf4j.Slf4j;
import pub.tbc.api.cache.CacheProvider;
import pub.tbc.api.cache.ServiceCache;
import pub.tbc.api.service.ServiceInfo;
import pub.tbc.api.service.mapping.NativeConfigServiceMapping;
import pub.tbc.api.service.mapping.ServiceMapping;

/**
 * 服务有效性过滤：是否能查到该服务的地址<br>
 * 调用服务解析组件获取服务信息对象，然后调用服务缓存判断该服务的地址是否已缓存，如果是，则通过，否则返回错误信息；<br>
 *
 * @author tbc on 2017/3/22 13:41:38.
 */
@Slf4j
public class ServiceValidityFilter extends AbstractHttpInboundHandler {
    private ServiceCache cache = CacheProvider.getServiceCache();
    private ServiceMapping serviceMapping = new NativeConfigServiceMapping();

    @Override
    protected void channelRead0(ChannelHandlerContext context, FullHttpRequest request) throws Exception {
        ServiceInfo serviceInfo = serviceMapping.map(request);
        if (cache.containService(serviceInfo.getServiceName())) {
            context.fireChannelRead(context);
        }
    }
}
